package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.AdChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(AdChannelDto dto) {

        //1.检查参数

        if (dto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        //2.封装查询条件
        Page pageRequest = new Page<>(dto.getPage(), dto.getSize());
        //查询条件
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdChannel::getName,dto.getName());
        }

        //3.分页查询

        //参数1；分页条件  参数2；查询条件
        IPage<AdChannel> pageResult = page(pageRequest, queryWrapper);

        //4.封装响应结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal());

        responseResult.setData(pageResult.getRecords());

        return responseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {

        //1.检查参数
        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.补全实体类属性
        adChannel.setIsDefault(false);
        adChannel.setCreatedTime(new Date());

        //3.保存实体类
        save(adChannel);

        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {

        //1.检查参数
        if (adChannel == null || adChannel.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.执行修改
        updateById(adChannel);

        //3.返回结果
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(Integer id) {

        //1.检查参数
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.先查询 如果存在 执行删除
        AdChannel adChannel = getById(id);

        if (adChannel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道信息不存在");
        }
        removeById(id);


        //异常情况测试
//        if (id>30){
//            throw new CustomException(ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID));
//        }

        //3.返回结果
        return ResponseResult.okResult();
    }
}
